package com.waterworld.watch.chat.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.chat.bean.MemberSelectBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.PhotoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by nhuan on 2019/3/19.
 */

public class CreateGroupAdapter extends RecyclerView.Adapter<CreateGroupAdapter.MyHolder>{

    private Context mContext;
    private List<MemberSelectBean> mList;

    public CreateGroupAdapter(Context context, List<MemberSelectBean> list) {
        mContext = context;
        mList = list;
    }

    public List<MemberSelectBean> getData() {
        return  mList;
    }
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_select_member,viewGroup,false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CreateGroupAdapter.MyHolder myHolder, int i) {
        if (mList.get(i).getGroupAllowUserBean().getHead() == null) {
            Glide.with(mContext).load(R.drawable.headimage_default).into(myHolder.iv_watch_avatar);
        } else if (mList.get(i).getGroupAllowUserBean().getHead().startsWith("sys")) {
            if (mList.get(i).getGroupAllowUserBean().getUserType() == 1) {
                Glide.with(mContext).load(PhotoUtils.getResource(mList.get(i).getGroupAllowUserBean().getHead().substring(0, 13))).into(myHolder.iv_watch_avatar);
            } else if (mList.get(i).getGroupAllowUserBean().getUserType() == 2) {
                Glide.with(mContext).load(PhotoUtils.getResource(mList.get(i).getGroupAllowUserBean().getHead().substring(0, 12))).into(myHolder.iv_watch_avatar);
            }
        } else {
            Glide.with(mContext).load(UrlContants.FILE_URL + "resources/watch/" + mList.get(i).getGroupAllowUserBean().getHead()).into(myHolder.iv_watch_avatar);
        }
        if (mList.get(i).getGroupAllowUserBean().getName() != null) {
            myHolder.tv_watch_name.setText(mList.get(i).getGroupAllowUserBean().getName());
        }
        if (mList.get(i).getGroupAllowUserBean().getPhone() != null) {
            myHolder.tv_watch_phone_number.setText(mList.get(i).getGroupAllowUserBean().getPhone());
        }
        myHolder.cb_selected.setChecked(mList.get(i).getSelect());
        myHolder.rl_watch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mList.get(i).setSelect(!mList.get(i).getSelect());
                myHolder.cb_selected.setChecked(mList.get(i).getSelect());
            }
        });
        myHolder.cb_selected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mList.get(i).setSelect(!mList.get(i).getSelect());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_watch)
        RelativeLayout rl_watch;

        @BindView(R.id.iv_watch_avatar)
        CircleImageView iv_watch_avatar;

        @BindView(R.id.tv_watch_name)
        TextView tv_watch_name;

        @BindView(R.id.tv_watch_phone_number)
        TextView tv_watch_phone_number;

        @BindView(R.id.cb_selected)
        CheckBox cb_selected;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
